package com.ruoyi.project.module.lyx;

import com.ruoyi.common.core.utils.DateUtils;
        import com.ruoyi.common.core.utils.poi.ExcelUtil;
        import com.ruoyi.common.core.web.controller.BaseController;
        import com.ruoyi.common.core.web.domain.AjaxResult;
        import com.ruoyi.common.core.web.page.TableDataInfo;
        import com.ruoyi.common.log.annotation.Log;
        import com.ruoyi.common.log.enums.BusinessType;
        import com.ruoyi.project.domain.YqgBarBills;
        import com.ruoyi.project.domain.YqgConsumptionDetails;
        import com.ruoyi.project.domain.YqgHands;
        import com.ruoyi.project.domain.YqgInformationRoom;
        import com.ruoyi.project.service.IYqgBarBillsService;
        import com.ruoyi.project.service.IYqgConsumptionDetailsService;
        import com.ruoyi.project.service.IYqgHandsService;
        import com.ruoyi.project.service.IYqgInformationRoomService;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.web.bind.annotation.*;

        import javax.servlet.http.HttpServletResponse;
        import java.util.Date;
        import java.util.List;

/**
 * 消费详细Controller
 *
 *
 * @author dr
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/lyx/chaxun")

public class LyxYqgXiebachaxunController extends BaseController
{
        @Autowired
        private IYqgConsumptionDetailsService yqgConsumptionDetailsService;
        @Autowired
        private IYqgBarBillsService yqgBarBillsService;
        @Autowired
        private IYqgHandsService yqgHandsService;
        @Autowired
        private IYqgInformationRoomService yqgInformationRoomService;

        /**
         * 查询消费详细列表
         */

        //@RequiresPermissions("project:details:list")
        @GetMapping("/list")
        public TableDataInfo list(YqgConsumptionDetails yqgConsumptionDetails) {
//                startPage();
                List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
                return getDataTable(list);
        }





        /**
         * 导出消费详细列表
         */
        //@RequiresPermissions("project:details:export")
        @Log(title = "导出", businessType = BusinessType.EXPORT)
        @PostMapping("/export")
        public void export(HttpServletResponse response, YqgConsumptionDetails yqgConsumptionDetails) {
                List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
                ExcelUtil<YqgConsumptionDetails> util = new ExcelUtil<YqgConsumptionDetails>(YqgConsumptionDetails.class);
                util.exportExcel(response, list, "消费详细数据");
        }

        /**
         * 获取消费详细详细信息
         */
        //@RequiresPermissions("project:details:query")
        @GetMapping(value = "/{ID}")
        public AjaxResult getInfo(@PathVariable("ID") Long ID) {
                return success(yqgConsumptionDetailsService.selectYqgConsumptionDetailsByID(ID));
        }

        /**
         * 新增消费详细
         */
        //@RequiresPermissions("project:details:add")
        @Log(title = "新增", businessType = BusinessType.INSERT)
        @PostMapping
        public AjaxResult add(@RequestBody YqgConsumptionDetails yqgConsumptionDetails) {
                return toAjax(yqgConsumptionDetailsService.insertYqgConsumptionDetails(yqgConsumptionDetails));
        }

        /**
         * 修改消费详细
         */
        //@RequiresPermissions("project:details:edit")
        @Log(title = "修改", businessType = BusinessType.UPDATE)
        @PutMapping
        public AjaxResult edit(@RequestBody YqgConsumptionDetails yqgConsumptionDetails) {

                YqgBarBills yqgBarBills = new YqgBarBills();
                YqgHands yqgHands = new YqgHands();
                YqgInformationRoom yqgInformationRoom = new YqgInformationRoom();
                //被并手牌号（不付钱那个）
                yqgBarBills.setHandBrand(yqgConsumptionDetails.getHandBrand());
                yqgHands.setHandBrand(yqgConsumptionDetails.getHandBrand());
                yqgInformationRoom.setHandBrand(yqgConsumptionDetails.getHandBrand());
                //并牌手牌号（付钱的那个）
                String mergehandsID = yqgConsumptionDetails.getMergeHands();
                Date drawtime = DateUtils.getNowDate();
                //获取并牌手牌号（付钱的那个）的吧台账单ID
                YqgBarBills yqgBarBillsMerge = new YqgBarBills();
                yqgBarBillsMerge.setHandBrand(mergehandsID); // 设置手牌号
                yqgBarBillsMerge.setHandState("1"); // 设置状态（在台）
                List<YqgBarBills> mergeList = yqgBarBillsService.selectYqgBarBillsList(yqgBarBillsMerge); // 查库
                Long barId = mergeList.get(0).getID(); // 获取吧台账号ID
                //置空
                yqgConsumptionDetails.setMergeHands("");
                yqgConsumptionDetails.setHandState("1");
                //修改消费详细表
                List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
                if (list.size() != 0) {
                        //批量修改
                        for (int i = 0; i < list.size(); i++) {
                                list.get(i).setMergeBarBillsId(barId);
                                list.get(i).setDrawTime(drawtime);
                                list.get(i).setMergeHands(mergehandsID);
                                list.get(i).setMergeStatus("0");
                        }
                        //批量更新
                        for (int i = 0; i < list.size(); i++) {
                                yqgConsumptionDetailsService.updateYqgConsumptionDetails(list.get(i));
                        }
                }


                List<YqgHands> handlist = yqgHandsService.selectYqgHandsList(yqgHands);
                if (handlist.size() != 0) {
                        //更新手牌表
                        yqgHands = handlist.get(0);
                        yqgHands.setHandState("3");
                        yqgHandsService.updateYqgHands(yqgHands);
                }

                //手牌状态为在台
                yqgBarBills.setHandState("1");
                List<YqgBarBills> billlist = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);
                if (billlist.size() != 0) {
                        //更新吧台账单表
                        yqgBarBills = billlist.get(0);
                        yqgBarBills.setMergeStatus("0");
                        yqgBarBillsService.updateYqgBarBills(yqgBarBills);
                }
                //更新房间信息表
                yqgInformationRoom.setHandState("1");
                List<YqgInformationRoom> roomlist = yqgInformationRoomService.selectYqgInformationRoomList(yqgInformationRoom);
                if (roomlist.size() != 0) {
                        //更新房间信息表
                        yqgInformationRoom = roomlist.get(0);
                        yqgInformationRoom.setHandState("3");
                        yqgInformationRoomService.updateYqgInformationRoom(yqgInformationRoom);
                }

                return new AjaxResult(200, "操作成功");
        }

        /**
         * 删除消费详细
         */
        //@RequiresPermissions("project:details:remove")
        @Log(title = "删除", businessType = BusinessType.DELETE)
        @DeleteMapping("/{IDs}")
        public AjaxResult remove(@PathVariable Long[] IDs) {
                return toAjax(yqgConsumptionDetailsService.deleteYqgConsumptionDetailsByIDs(IDs));
        }
}
